package com.xyoto.community.task;

import cn.hutool.core.date.DateTime;
import com.xyoto.community.entity.main.HotTopic;
import com.xyoto.community.enums.TopicEnum;
import com.xyoto.community.exception.ExceptionUtils;
import com.xyoto.community.mapper.main.DynamicMapper;
import com.xyoto.community.mapper.main.HotTopicMapper;
import com.xyoto.community.mapper.main.TopicFollowMapper;
import com.xyoto.community.po.TopicHotNumPO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import java.util.*;

/**
 * 实现热门话题，定时更新
 */
@Component
@Slf4j
public class HotTopicTask {

    @Autowired
    private HotTopicMapper hotTopicMapper;

    @Autowired
    private TopicFollowMapper topicFollowMapper;

    @Autowired
    private DynamicMapper dynamicMapper;

    @Autowired
    private HotTopicRedisImpl hotTopicRedis;

    @Scheduled(cron ="0 30 3 1/2 * ?")
    private void timingUpdateHotTopic(){

        log.info("开始执行更新热门话题功能");
        List<TopicHotNumPO> list = hotTopicMapper.getAllHotTopic();

        List<TopicHotNumPO> follows = topicFollowMapper.getHotTopicFollow(list);

        List<TopicHotNumPO> dynamics = dynamicMapper.getHotTopicDynamicCount(list);

        Map<Integer, TopicHotNumPO> map = new HashMap<>();

        // 通过相同的topicId进行匹配
        for (TopicHotNumPO topicHotNumPO : list) {
            map.put(topicHotNumPO.getTopicId(),topicHotNumPO);
        }

        for (TopicHotNumPO follow : follows) {
            Integer topicId = follow.getTopicId();
            TopicHotNumPO topicHotNumPO = map.get(topicId);
            if (topicHotNumPO == null) continue;
            if (StringUtils.isEmpty(follow.getTotalHot())){
                follow.setTotalHot(topicHotNumPO.getTotalHot());
            }
            map.put(follow.getTopicId(),follow);
        }
        for (TopicHotNumPO dynamic : dynamics) {
            Integer topicId = dynamic.getTopicId();
            TopicHotNumPO topicHotNumPO = map.get(topicId);
            if (topicHotNumPO == null) continue;
            if (StringUtils.isEmpty(dynamic.getTopicFollowCount())){
                dynamic.setTopicFollowCount(topicHotNumPO.getTopicFollowCount());
            }
            if (StringUtils.isEmpty(dynamic.getTotalHot())){
                dynamic.setTotalHot(topicHotNumPO.getTotalHot());
            }
            map.put(topicId,dynamic);
        }
        List<HotTopic> hotTopics = new ArrayList<>();
        // 将数据放入hotTopics中
        for (TopicHotNumPO hotTopic : list) {
            TopicHotNumPO topicHotNumPO = map.get(hotTopic.getTopicId());
            HotTopic hotTopic1 = new HotTopic();
            Integer topicDynamicCount = topicHotNumPO.getTopicDynamicCount();
            Integer topicFollowCount = topicHotNumPO.getTopicFollowCount();
            topicDynamicCount = topicDynamicCount == null ? 0:topicDynamicCount;
            topicFollowCount = topicFollowCount == null ? 0:topicFollowCount;
            int currentHot = topicDynamicCount + topicFollowCount - hotTopic.getTotalHot();
            int totalHot = topicDynamicCount + topicFollowCount;
            currentHot = Math.max(currentHot,0);
            hotTopic1.setTotalHot(totalHot);
            //将当前热度值和topicId进行添加
            hotTopic1.setCurrentHot(currentHot);
            hotTopic1.setTopicId(topicHotNumPO.getTopicId());
            hotTopic1.setModifiedTime(new DateTime());
            //放到最终的list
            hotTopics.add(hotTopic1);
        }

        log.info("开始执行热门话题更新SQL");
        Integer updateHotTopic = hotTopicMapper.updateHotTopic(hotTopics);
        if (updateHotTopic == null || updateHotTopic == 0) ExceptionUtils.error(TopicEnum.HOT_TOPIC_UPDATE_FAIL);

        log.info("开始将热门话题存入redis");
        hotTopicRedis.saveHotTopic(hotTopics);

    }

}
